"use strict";
const redisManager = require('./redisManager');
const CONFIG = require('../../../../../common/config');
const moment = require('moment');
const async = require('async');
const luaUtil = require('../commonUtils/luaUtil');

const docConvertUtil = (function () {

    function docConvertUtil() {
    }

    /**
     * 转换 API docString 到 openApi 的 doc 模型(提取可替换数组)
     *
     * @author 王俊
     * @date 2018-06-03
     *
     *
     * @param {String} docString API 的文档模型字符串
     * @param {Function} callback 回调函数
     *                      error: 转换失败
     *                      results: 转换后的数组
     */
    docConvertUtil.covertApiDocStringToOpenApiDoc = function (docString, callback) {

        let apiDoc = null;
        let openDoc = null;
        let error = null;

        try {
            apiDoc = JSON.parse(docString);
            openDoc = [];
            for (let  i=0; i<apiDoc.coreStructure.root.children.length; i++) {
                const apiNode = apiDoc.coreStructure.root.children[i];
                if([1,2,7].indexOf(apiNode.type) < 0) continue;
                const openNode = {
                    id : apiNode.id,
                    type : apiNode.type,
                };
                // 文字
                if(apiNode.type === 1) {
                    openNode.max = apiNode.value.max;
                    openNode.text = apiNode.value.value;
                }
                // 图片
                if(apiNode.type === 2) {
                    openNode.image = apiNode.value.value;
                    openNode.real_w = apiNode.value.pos.w;
                    openNode.real_h = apiNode.value.pos.h;
                    openNode.w = apiNode.value.mask.w;
                    openNode.h = apiNode.value.mask.h;
                }
                // 视频
                if(apiNode.type === 7) {
                    openNode.max_duration = apiNode.relations[0].duration;
                    openNode.video = apiNode.value.url;
                    openNode.w = apiNode.value.mask.w;
                    openNode.h = apiNode.value.mask.h;
                    openNode.real_w = apiNode.value.pos.w;
                    openNode.real_h = apiNode.value.pos.h;
                    openNode.real_duration = apiNode.value.duration;
                }
                openDoc.push(openNode);
            }
        } catch (e) {
            console.log(__dirname + '/' + __filename + ' covertApiDocStringToOpenApiDoc error: ' + error);
            error = e;
        } finally {
            callback(error, openDoc);
        }
    };

    /**
     * 合并 openApi doc 到 api 的 docString 模型中
     *
     * @author 王俊
     * @date 2018-06-03
     *
     * @param {String} docString API 中的文档模型字符串
     * @param {String} openApiDoc openApi 使用的模型数组
     * @param {Function} callback 回调函数
     *                      err: 合并失败
     *                      docString: 合并后的模型 json
     */
    docConvertUtil.combineApiDocStringWithOpenApiDoc = function(docString, openApiDoc, callback) {

        let apiDoc = null;
        let apiDocString = null;
        let openNodeMap = null;
        let error = null;
        try {
            apiDoc = JSON.parse(docString);
            openApiDoc = JSON.parse(openApiDoc);
            openNodeMap = {};

            for (let i=0; i<openApiDoc.length; i++) {
                const openNode = openApiDoc[i];
                openNodeMap[openNode.id] = openNode;
            }

            for (let  i=0; i<apiDoc.coreStructure.root.children.length; i++) {
                const apiNode = apiDoc.coreStructure.root.children[i];
                apiNode.type = Number(apiNode.type);
                if([1,2,7].indexOf(apiNode.type) < 0) continue;
                const openNode = openNodeMap[apiNode.id];
                if(!openNode) continue;

                // 文字
                if(apiNode.type === 1) {
                     apiNode.value.value = openNode.text;
                }
                // 图片
                if(apiNode.type === 2) {
                    apiNode.value.value = openNode.image;
                    apiNode.value.pos.w = openNode.real_w;
                    apiNode.value.pos.h = openNode.real_h;
                }
                // 视频
                if(apiNode.type === 7) {
                    apiNode.value.url = openNode.video;
                    apiNode.value.pos.w = openNode.real_w;
                    apiNode.value.pos.h = openNode.real_h;
                    apiNode.value.duration = openNode.real_duration;
                }
            }
            apiDocString = JSON.stringify(apiDoc);

        } catch (e) {
            console.log(__dirname + '/' + __filename + ' covertApiDocStringToOpenApiDoc error: ' + error);
            error = e;
        } finally {
            callback(error, apiDocString);
        }
    };



    return docConvertUtil;
})();

module.exports = docConvertUtil;